local skel = fk.CreateSkill{
  name = "est__yanhe",
}
--local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["est__yanhe"] = "言壑",
  [":est__yanhe"] = "结束阶段，你可以将武将牌上的牌置入弃牌堆，然后你可以弃置至多等量名角色区域内各一张牌，"..
  "以此法弃置牌花色与你相同的角色不能使用牌指定你为目标直到其回合结束。",
  ["#est__yanhe-choose"] = "言壑：你可以弃置至多 %arg 名角色区域内各一张牌",
  ["@@est__yanhe_prohibit"] = "言壑",
}
---@type TrigSkelSpec<PhaseFunc>
local yanhe = {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player.phase == Player.Finish and #player:getPile("est__zhuxing") > 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cids = player:getPile("est__zhuxing")
    room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, nil, true, player)
    if not player.dead then
      local num = #cids
      local disc = {}
      local targets = room:askToChoosePlayers(player, {
        targets = table.filter(room:getAlivePlayers(), function(p)
          if p ~= player then
            return #p:getCardIds("hej") > 0
          else
            ---@diagnostic disable-next-line: return-type-mismatch
            return table.find(p:getCardIds("he"), function (id)
              return not player:prohibitDiscard(id)
            end) or #p:getCardIds("j") > 0
          end
        end),
        min_num = 1,
        max_num = num,
        skill_name = skel.name,
        prompt = "#est__yanhe-choose:::" .. tostring(num)
      })
      if #targets > 0 then
        room:sortByAction(targets)
        for _, p in ipairs(targets) do
          if player.dead then goto finish end
          if not p.dead and #p:getCardIds("hej") > 0 then
            local cid = room:askToChooseCard(player, {
              target = p,
              flag = "hej",
              skill_name = skel.name
            })
            if cid then
              disc[p.id] = Fk:getCardById(cid).suit
              room:throwCard(cid, skel.name, p, player)
            end
          end
        end
        if not player.dead and disc[player.id] then
          local p_suit = disc[player.id]
          for id, suit in pairs(disc) do
            if suit == p_suit and id ~= player.id then
              local p = room:getPlayerById(id)
              if not p.dead then
                room:setPlayerMark(p, "@@est__yanhe_prohibit", 1)
              end
            end
          end
        end
      end
      ::finish::
    end
  end
}
---@type ProhibitSpec
local yanhe_prohibit = {
  is_prohibited = function (self, from, to, card)
    return from:getMark("@@est__yanhe_prohibit") > 0 and to:hasSkill(skel.name, true)
  end
}
---@type TrigSkelSpec<TurnFunc>
local yanhe_delay = {
  can_refresh = function (self, event, target, player, data)
    return player == target and target:getMark("@@est__yanhe_prohibit") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@est__yanhe_prohibit", 0)
  end
}
skel:addEffect(fk.EventPhaseStart, yanhe)
skel:addEffect("prohibit", yanhe_prohibit)
skel:addEffect(fk.TurnEnd, yanhe_delay)
return skel